//
//  HttpBaseTool.m
//  easymarketing
//
//  Created by HailongHan on 14/12/22.
//  Copyright (c) 2014年 cubead. All rights reserved.
//

#import "HttpClient.h"
#import "MJExtension.h"
#import "HttpTool.h"

@implementation HttpClient

+ (void)getWithUrl:(NSString *)url param:(id)param resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    NSDictionary *params = [param keyValues];
    
    [HttpTool get:url params:params success:^(id responseObj) {
        if (success) {
            id result = [resultClass objectWithKeyValues:responseObj];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+(void)getWithUrl:(NSString *)url token:(NSString *)token param:(id)param resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    url = [url stringByAppendingString:[NSString stringWithFormat:@"?token=%@",token]];
    
    NSDictionary *params = [param keyValues];
    [HttpTool get:url params:params success:^(id responseObj) {
        if (success) {
            id result = [resultClass objectWithKeyValues:responseObj];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];

}

+(void)getWithUrl:(NSString *)url paramDic:(NSDictionary *)param resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    [HttpTool get:url params:param success:^(id responseObj) {
        if (success) {
            id result = [resultClass objectWithKeyValues:responseObj];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+(void)getWithUrl:(NSString *)url token:(NSString *)token paramDic:(NSDictionary *)param resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    url = [url stringByAppendingString:[NSString stringWithFormat:@"?token=%@",token]];
    
    [HttpTool get:url params:param success:^(id responseObj) {
        if (success) {
            id result = [resultClass objectWithKeyValues:responseObj];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+(void)postWithUrl:(NSString *)url param:(id)param resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    //构建字典参数
    NSDictionary *params = [param keyValues];
    
    [HttpTool post:url params:params success:^(id responseObj) {
        if (success) {
            id result = [resultClass objectWithKeyValues:responseObj];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+(void)postWithUrl:(NSString *)url token:(NSString *)token param:(id)param resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    url = [url stringByAppendingString:[NSString stringWithFormat:@"?token=%@",token]];
    
    //构建字典参数
    NSDictionary *params = [param keyValues];
    
    [HttpTool post:url params:params success:^(id responseObj) {
        if (success) {
            id result = [resultClass objectWithKeyValues:responseObj];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];

}

+(void)postWithUrl:(NSString *)url paramDic:(NSDictionary *)param resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    [HttpTool post:url params:param success:^(id responseObj) {
        if (success) {
            id result = [resultClass objectWithKeyValues:responseObj];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+(void)postWithUrl:(NSString *)url token:(NSString *)token paramDic:(NSDictionary *)param resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    url = [url stringByAppendingString:[NSString stringWithFormat:@"?token=%@",token]];
    
    [HttpTool post:url params:param success:^(id responseObj) {
        if (success) {
            id result = [resultClass objectWithKeyValues:responseObj];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+(void)putWithUrl:(NSString *)url token:(NSString *)token paramDic:(NSDictionary *)param resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    url = [url stringByAppendingString:[NSString stringWithFormat:@"?token=%@",token]];
    
    [HttpTool put:url params:param success:^(id responseObj) {
        if (success) {
            id result = [resultClass objectWithKeyValues:responseObj];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

//+(void)postUploadImage:(NSString*)urlStr andMimeType:(NSString*)mimeType andData:(NSData *)data resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure{
//
//    [HttpTool postUploadImage:urlStr andMimeType:mimeType andData:data success:^(id responseObj) {
//        if (success) {
//            id result = [resultClass objectWithKeyValues:responseObj];
//            success(result);
//        }
//    } failure:^(NSError *error) {
//        if (failure) {
//            failure(error);
//        }
//    }];
//}

+(void)postUploadImage:(NSString*)url andMimeType:(NSArray*)mimeType andData:(NSArray *)data resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    [HttpTool postUploadImage:url andMimeType:mimeType andData:data success:^(id responseObj) {
        if (success) {
            id result = [resultClass objectWithKeyValues:responseObj];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}


+(void)putWithUrl:(NSString *)url paramDic:(NSDictionary *)param resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    [HttpTool put:url params:param success:^(id responseObj) {
        if (success) {
            id result = [resultClass objectWithKeyValues:responseObj];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+(void)deleteWithUrl:(NSString *)url token:(NSString *)token paramDic:(NSDictionary *)param resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    url = [url stringByAppendingString:[NSString stringWithFormat:@"?token=%@",token]];
    
    [HttpTool delete:url params:param success:^(id responseObj) {
        if (success) {
            id result = [resultClass objectWithKeyValues:responseObj];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}




@end
